<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="layuimini/css/public.css" media="all">
    <script src="layuimini/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="id" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-normal"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="submit" id="refresh-btn" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-reset-btn"><i class="layui-icon layui-icon-refresh-1"></i> 重 置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>

<script>
    //定义一个刷新页面的函数，让子页面调用这个方法来刷新页面
    function refreshTable(){
        $("#refresh-btn").click();
    }

    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: 'member/listAllMember',
            toolbar: '#toolbarDemo',
            cols: [ //这里两个中括号不能并排，不然会出错
                [
                    {type: "checkbox", width: 50},
                    {field: 'tId', width: 75, title: '编号'},
                    {field: 'tLevel', width: 125, title: '会员等级'},
                    {field: 'tDeduction', width: 125, title: '则扣率', sort: true},
                    {field: 'tDescribe', width: 125, title: '详细描述'},
                    {title: '操作', minWidth: 100, toolbar: '#currentTableBar', align: "center"}
                ]
            ],
            limits: [5, 10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line',
            //用于对分页请求的参数：page、limit重新设定名称
            request:{
                pageName: 'pageNo',
                limitName: 'pageSize'
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var onData = data.field;        // 获取监听事件中的操作对象
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    // 请求地址中的请求参数条件
                    id: onData.id
                }
            }, 'data');

            return false;
        });

        // 监听重置操作
        form.on('submit(data-reset-btn)', function (data) {
            //清空表单内容
            $("#id").reset();
            $("#province").reset();
            layui.form.render();
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    // 请求地址中的请求参数条件
                    id: null
                }

            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '新增会员等级',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'table/memberAdd',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var listId = [];
                for(var i = 0; i < data.length; i++){
                    listId.push(data[i].tId);
                }
                var dat = {
                    'ids': listId
                }
                var da = JSON.stringify(dat);

                console.log(da);

                layer.confirm('确定删除所选行么', function () {
                    $.ajax({
                        url:"member/deleteMember",
                        method: "post",
                        data: da,
                        contentType: "application/json;charset=UTF-8",   // 注意：这里需要设置请求头，因为springmvc中默认接收的请求头为这个
                        dataType: "json",
                        success: function (res){
                            if(res.code == 0){
                                location.reload('currentTableId');
                            }else{
                                layer.msg("出了点小状况，请稍后再试",{ icon: 2,time: 1000,anim: 6});
                            }
                        }
                    })
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '修改会员等级内容',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'table/memberEdit/' + data.tId
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                //定义请求参数
                let idList = [];
                idList.push(data.tId);
                //加包一层，因为后端使用@RequestBody studentBo bo接收参数
                var dat = {
                    'ids': idList
                }
                //这里需要转为JSON格式
                var da = JSON.stringify(dat);

                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        url:"member/deleteMember",
                        method: "post",
                        data: da,
                        contentType: "application/json;charset=UTF-8",   // 注意：这里需要设置请求头，因为springmvc中默认接收的请求头为这个
                        dataType: "json",
                        success: function (res){
                            if(res.code == 0){
                                obj.del();
                                layer.close(index);
                            }else{
                                layer.msg("出了点小状况，请稍后再试",{ icon: 2,time: 1000,anim: 6});
                            }
                        }
                    })

                });
            }
        });

    });
</script>

</body>
</html>